
// Definition for singly-linked list.
class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val===undefined ? 0 : val)
        this.next = (next===undefined ? null : next)
    }
}


function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
    if(head!.next==null){
        return null;
    }

   

    console.log(`${head!.val}`)
    
    let length = 1;
    while(head!.next!=null){
        length++;
    }

    let targetIndex = length - n;
    length = 0;
    let currNode:ListNode|null=head;
   
    while(true){
        if(length == targetIndex){
            //此节点就是
            currNode!.val = currNode!.next?.val??0;
            currNode!.next=currNode!.next?.next??null;
            break;
        }

        length++;
       
        if(currNode!.next!.next==null){
            currNode!.next = null;
            break
        }else{
             currNode = currNode!.next;
        }   
    }



    return head;
    
};

/**
 * 解法二
 * 双指针 快慢指针
 * 快指针先走n步，最后慢指针所在的位置就是要删除节点
 */